/**
 @file ctc_tmx_flexe.c

 @author  Copyright (C) 2011 Centec Networks Inc.  All rights reserved.

 @date 2010-07-08

 @version v1.0

*/

#include "sal.h"
#include "ctc_const.h"
#include "ctc_error.h"
#include "ctc_debug.h"
#include "ctc_flexe.h"

#include "sys_usw_common.h"
#include "sys_usw_flexe.h"

/**
 @brief   Enable flexe function

 @param[in] lchip    local chip id

 @param[in] serdes_id    serdes id

 @param[in]  enable   enable or disable flexe function

 @remark[TM2] Enable or disable flexe function

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_set_en(uint8 lchip, uint16 serdes_id, uint8 enable)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_set_en(lchip, serdes_id, enable));
    }

    return CTC_E_NONE;
}

/**
 @brief   Get flexe enable function

 @param[in] lchip    local chip id

 @param[in] serdes_id    serdes id

 @param[out]  enable   enable or disable flexe function

 @remark[TM2] Get Enable or disable flexe function

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_get_en(uint8 lchip, uint16 serdes_id, uint8* enable)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_get_en(lchip, serdes_id, enable));
    }

    return CTC_E_NONE;
}
/**
 @brief   Create flexe group

 @param[in] lchip    local chip id

 @param[in] group_id    group id

 @param[in]  p_group   group param sturcture pointer

 @remark[TM2] Create flexe group

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_create_group(uint8 lchip, uint32 group_id, ctc_flexe_grp_t* p_group)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_create_group(lchip, group_id, p_group));
    }

    return CTC_E_NONE;
}

/**
 @brief   Destroy flexe group

 @param[in] lchip    local chip id

 @param[in] group_id    group id

 @remark[TM2] Destroy flexe group

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_destroy_group(uint8 lchip, uint32 group_id)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_destroy_group(lchip, group_id));
    }

    return CTC_E_NONE;
}

/**
 @brief   Get flexe group information

 @param[in] lchip    local chip id

 @param[in] group_id    group id

 @param[out]  p_group   group information sturcture pointer

 @remark[TM2] Get flexe group information

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_get_group(uint8 lchip, uint32 group_id, ctc_flexe_grp_t* p_group)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_get_group(lchip, group_id, p_group));
    }

    return CTC_E_NONE;
}

/**
 @brief   Set flexe group property

 @param[in] lchip    local chip id

 @param[in] group_id    group id

 @param[in] group_prop    group property

 @param[in] p_value   property value

 @remark[TM2] Set flexe group property

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_set_group_property(uint8 lchip, uint32 group_id, ctc_flexe_grp_prop_t group_prop, void* p_value)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_set_group_property(lchip, group_id, group_prop, p_value));
    }

    return CTC_E_NONE;
}

/**
 @brief   Get flexe group property

 @param[in] lchip    local chip id

 @param[in] group_id    group id

 @param[in] group_prop    group property

 @param[out] p_value   property value

 @remark[TM2] Get flexe group property

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_get_group_property(uint8 lchip, uint32 group_id, ctc_flexe_grp_prop_t group_prop, void* p_value)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_get_group_property(lchip, group_id, group_prop, p_value));
    }

    return CTC_E_NONE;
}

/**
 @brief   Add flexe client

 @param[in] lchip    local chip id

 @param[in] client_id    client id

 @param[in]  p_client   client param sturcture pointer

 @remark[TM2] Add flexe client

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_add_client(uint8 lchip, uint32  client_id, ctc_flexe_client_t* p_client)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_add_client(lchip, client_id, p_client));
    }

    return CTC_E_NONE;
}

/**
 @brief   Remove flexe client

 @param[in] lchip    local chip id

 @param[in] client_id    client id

 @remark[TM2] Remove flexe client

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_remove_client(uint8 lchip, uint32  client_id)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_remove_client(lchip, client_id));
    }

    return CTC_E_NONE;
}

/**
 @brief   Get flexe client

 @param[in] lchip    local chip id

 @param[in] client_id    client id

 @param[out]  p_client   client information sturcture pointer

 @remark[TM2] Get flexe client

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_get_client(uint8 lchip, uint32  client_id, ctc_flexe_client_t* p_client)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_get_client(lchip, client_id, p_client));
    }

    return CTC_E_NONE;
}

/**
 @brief   Set flexe client property

 @param[in] lchip    local chip id

 @param[in] client_id    client id

 @param[in] client_prop    client property

 @param[in] p_value   property value

 @remark[TM2] Set flexe client property

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_set_client_property(uint8 lchip, uint32  client_id, ctc_flexe_client_prop_t client_prop, void* p_value)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_set_client_property(lchip, client_id, client_prop, p_value));
    }

    return CTC_E_NONE;
}

/**
 @brief   Get flexe client property

 @param[in] lchip    local chip id

 @param[in] client_id    client id

 @param[in] client_prop    client property

 @param[out] p_value   property value

 @remark[TM2] Get flexe client property

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_get_client_property(uint8 lchip, uint32  client_id, ctc_flexe_client_prop_t client_prop, void* p_value)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_get_client_property(lchip, client_id, client_prop, p_value));
    }

    return CTC_E_NONE;
}

/**
 @brief Get client link up status

 @param[in] lchip    local chip id

 @param[in]  client_id    client id

 @param[out] p_is_up  0 means not link up, 1 means link up

 @remark[TM2]  Get client link up status

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_get_client_link_up(uint8 lchip, uint32  client_id, bool* p_is_up)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_get_client_link_up(lchip, client_id, p_is_up));
    }

    return CTC_E_NONE;
}

/**
 @brief Set felxe client cross connect

 @param[in] lchip    local chip id

 @param[in]  p_cross   cross connect sturcture pointer

 @remark[TM2]  Set felxe client cross connect

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_set_client_cross(uint8 lchip, ctc_flexe_cross_t* p_cross)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_set_client_cross(lchip, p_cross));
    }

    return CTC_E_NONE;
}

/**
 @brief Get felxe client cross connect

 @param[in] lchip    local chip id

 @param[out]  p_cross   cross connect sturcture pointer

 @remark[TM2]  Get felxe client cross connect

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_get_client_cross(uint8 lchip, ctc_flexe_cross_t* p_cross)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_get_client_cross(lchip, p_cross));
    }

    return CTC_E_NONE;
}

/**
 @brief   Set flexe phy property

 @param[in] lchip    local chip id

 @param[in] serdes_id    serdes id

 @param[in] phy_prop    phy property

 @param[in] p_value   property value

 @remark[TM2] Set flexe phy property

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_set_phy_property(uint8 lchip, uint16 serdes_id, ctc_flexe_phy_prop_t phy_prop, void* p_value)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_set_phy_property(lchip, serdes_id, phy_prop, p_value));
    }

    return CTC_E_NONE;
}

/**
 @brief   Get flexe phy property

 @param[in] lchip    local chip id

 @param[in] serdes_id    serdes id

 @param[in] phy_prop    phy property

 @param[out] p_value   property value

 @remark[TM2] Get flexe phy property

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_get_phy_property(uint8 lchip, uint16  serdes_id, ctc_flexe_phy_prop_t phy_prop, void* p_value)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_get_phy_property(lchip, serdes_id, phy_prop, p_value));
    }

    return CTC_E_NONE;
}

/**
 @brief   Initialization for flexe module

 @param[in] lchip    local chip id

 @param[in] p_cfg global configuration for flexe pointer

 @remark[TM2] Initialization for flexe module

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_init(uint8 lchip, void* p_cfg)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_FLEXE);
    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_init(lchip, p_cfg));
    }

    return CTC_E_NONE;
}

/**
 @brief   Deinit for flexe module

 @param[in] lchip    local chip id

 @remark[TM2] Deinit for flexe module

 @return CTC_E_XXX

*/
int32
ctc_tmx_flexe_deinit(uint8 lchip)
{
    uint8 lchip_start              = 0;
    uint8 lchip_end                = 0;

    LCHIP_CHECK(lchip);

    CTC_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_flexe_deinit(lchip));
    }

    return CTC_E_NONE;
}

